<?php

namespace app\admin\model;


/**
 * Author:Jack Wang
 * Time:2018-11-12 15:43:00
 * Describe:案例分类模型
 * Class InformationClassification
 * @package app\admin\model
 */
class CaseClassification extends Base
{
    protected $autoWriteTimestamp = true;//开启自动添加时间戳
    protected $insert = ['create_id','create_nick_name'];//添加插入

    //添加人ID
    protected function setCreateIdAttr()
    {
        return $this->admin["admin_id"];
    }

    //添加人名称
    protected function setCreateNickNameAttr()
    {
        return !empty($this->admin["nick_name"]) ? $this->admin["nick_name"] : $this->admin["admin_name"];
    }


    /**
     * Author:Jack Wang
     * Time:2018-10-23 10:48:36
     * Describe:案例分类全部列表
     * @param string $keyword
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function classificationList($keyword = ""){
        return $this
            ->where(function ($q)use ($keyword){
                if(!empty($keyword)) {
                    $q->whereOr("classification_name",'like', "%" . $keyword . "%")->whereOr("create_nick_name",'like', "%" . $keyword . "%");
                }
            })
            ->where(function ($q){
                if(!empty($this->substations)){//是否设置了当前网站
                    $q->where("substation_id",$this->substations);
                }else{
                    if($this->admin["group_id"] != config("SuperAdminGroupId")) {//是否超级管理员
                        $arr = [];
                        $Substation = new Substation();//分站类
                        $userList = $Substation->userList();//获取分站集合
                        foreach ($userList as $val){
                            $arr[] = $val['substation_id'];
                        }
                        $q->where("substation_id","in",$arr);
                    }
                }
            })
            ->order("substation_id ASC,is_index DESC,sort DESC,update_time DESC,create_time DESC")
            ->select();
    }

    /**
     * Author:Jack Wang
     * Time:2018-10-23 14:10:58
     * Describe:获取案例分类案例数量
     * @param $classification_id
     * @return int|string
     */
    public function getInformationNumber($classification_id){
        return $this->table($this->prefix."case")->where("classification_id",$classification_id)->count();
    }

    //验证是否有权限操作
    public function checkSave($classification_id,$substation_id){
        $is = [false,false];//验证标识
        if($this->admin["group_id"] != config("SuperAdminGroupId")) {//是否超级管理员
            $Substation = new Substation();//分站类
            $userList = $Substation->userList();//获取分站集合
            $classification_substation_id = $this->where("classification_id",$classification_id)->value("substation_id");//获取原分站ID
            foreach ($userList as $val){//遍历分站集合
                if(!empty($classification_id)) {//修改验证
                    if ($classification_substation_id == $val["substation_id"]) {
                        $is[0] = true;
                    }
                }else{
                    //添加不验证
                    $is[0] = true;
                }
                if(!empty($substation_id)) {//添加修改验证
                    if ($substation_id == $val["substation_id"]) {
                        $is[1] = true;
                    }
                }else{
                    //删除不验证
                    $is[1] = true;
                }
            }
        }else{
            $is = [true,true];
        }
        return $is[0] === true && $is[1] === true ? true : false;
    }

}